package com.wanhe.base.util;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

/**
 * 工具类
 * 
 *
 */
public class FileUtil {

	public final static String DEFAULT_DIR = "wanhe";
	private final static String HEADER_NAME = "usrHeadimg.png";
	private final static String DIR_IMAGE_MAIN = "/imageMain";


	public static String getMimeType(String url) {
		String type = null;
		String extension = MimeTypeMap.getFileExtensionFromUrl(url);
		if (extension != null) {
			MimeTypeMap mime = MimeTypeMap.getSingleton();
			type = mime.getMimeTypeFromExtension(extension);
		}
		return type;
	}

	public static File saveImg(String path, String name, Bitmap bitmap)
			throws IOException {
		File file = new File(path, name + ".png");
		// 若图片文件在SD卡的文件夹不存在
		if (!file.getParentFile().exists()) {
			// 创建该文件夹
			file.getParentFile().mkdirs();
		}
		// 若文件不存在，则创建
		if (!file.exists()) {
			file.createNewFile();
		}
		// 创建文件输出流
		FileOutputStream out = new FileOutputStream(file);
		// 保存图片至SD卡指定文件夹
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
		return file;
	}

	/**
	 * 获取应用在SD卡的根目录
	 * 
	 * @param context
	 * @return
	 */
	public static File getDefaultStoragePath(Context context) {
		final String state = Environment.getExternalStorageState();
		String result = "";
		if (Environment.MEDIA_MOUNTED.equals(state)
				&& Environment.getExternalStorageDirectory().canWrite()) {
			result = Environment.getExternalStorageDirectory() + File.separator
					+ DEFAULT_DIR + File.separator;
		} else {
			result = context.getApplicationContext().getFilesDir()
					.getAbsolutePath()
					+ File.separator + DEFAULT_DIR;
		}
		File file = new File(result);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}


	public static String getImageCacheMain(Context mContext){
		String imageCache = getDefaultStoragePath(mContext)
				.getPath() + DIR_IMAGE_MAIN;
		createDir(imageCache);
		return imageCache;
	}




	/**
	 * 获取用户头像文件路径
	 * 
	 * @param context
	 * @return
	 * @throws IOException
	 */
	public static File getHeaderImgFile(Context context) throws IOException {
		return createOrReplaceFile(getDefaultStoragePath(context)
				.getAbsolutePath(), HEADER_NAME);

	}




	/**
	 * 生成文件
	 * 
	 * @param dir
	 *            文件所在路径
	 * @param name
	 *            文件名称
	 * @return
	 * @throws IOException
	 */
	public static final File createOrReplaceFile(String dir, String name)
			throws IOException {
		File file = new File(dir, name);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		return file;
	}

	/**
	 * 生成文件
	 * 
	 * @param path
	 *            文件路径
	 * @return
	 * @throws IOException
	 */
	public static final File createOrReplaceFile(String path)
			throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			if (file.mkdirs()) {
				file.createNewFile();
			}
		}
		return file;
	}

	/**
	 * 创建文件夹
	 * 
	 * @param path
	 * @return
	 */
	public static File createDir(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/**
	 * 检测文件是否存在
	 * 
	 * @param path
	 * @return
	 */
	public static boolean isFileExists(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}

		File file = new File(path);
		if (!file.exists()) {
			return false;
		}
		return true;
	}

	/**
	 * get file store size
	 * 
	 * @param filePath
	 * @return
	 */
	public static long getFileSize(String filePath) {
		long length = 0;
		if (TextUtils.isEmpty(filePath)) {
			return length;
		}

		File file = new File(filePath);
		if (file.exists()) {
			FileInputStream fis = null;
			try {
				fis = new FileInputStream(file);
				length = fis.available();
				return length;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} finally {
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			return 0;
		}
	}

	/**
	 * sd卡是否有足够的可以空间
	 * 
	 * @return boolean
	 * 
	 */
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static boolean isSdCardAvailale(long maxSize) {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			long size = 0;
			StatFs fs = new StatFs(Environment.getExternalStorageDirectory()
					.getPath());
			// Android API18之前：fs.getAvailableBlocks()*fs.getBlockSize()
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
				size = fs.getAvailableBytes() / 1024;
			} else {
				File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
				StatFs stat = new StatFs(path.getPath());
				long blockSize = stat.getBlockSize();
				long availableBlocks = stat.getAvailableBlocks();
				size = (availableBlocks * blockSize) / 1024;
			}
			if (size < maxSize) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}

	public static boolean isSdCardAvailale() {
		return isSdCardAvailale(500);
	}

	/**
	 * 插入图片到camera目录中
	 * 
	 * @param context
	 */
	@SuppressLint("SimpleDateFormat")
//	public static String interCameraImasge(Context context, String cameraPath,
//			int screenWidth, int screenHeight) {
//		try {
//			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
//			Date date = new Date();
//			String fileName = format.format(date);
//			fileName = "/IMG_" + fileName;
//
//			FileUtil.cameraPhotoRotate(cameraPath, screenWidth, screenHeight);
//			String imgPath = Images.Media.insertImage(
//					context.getContentResolver(), cameraPath, fileName,
//					fileName);
//			if (imgPath != null) {
//				String newPath = FileUtil.getUriToPath(Uri.parse(imgPath),
//						context);
//				if (newPath != null) {
//					Intent intent = new Intent(
//							Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//					Uri uri = Uri.fromFile(new File(newPath));
//					intent.setData(uri);
//					context.sendBroadcast(intent);
//				}
//				return newPath;
//			} else {
//				ContentValues values = new ContentValues(6);
//				values.put(Images.Media.DATE_MODIFIED,
//						System.currentTimeMillis() / 1000);
//				values.put(Images.Media.TITLE, fileName);
//				values.put(Images.Media.DISPLAY_NAME, fileName);
//				values.put(Images.Media.MIME_TYPE, "image/jpeg");
//				values.put(Images.Media.DATA, cameraPath);
//				values.put(Images.Media.SIZE,
//						FileUtil.getFileSize(cameraPath));
//				Uri result = context.getContentResolver().insert(
//						Images.Media.EXTERNAL_CONTENT_URI, values);
//				if (result != null) {
//					String newPath = FileUtil.getUriToPath(result, context);
//					return newPath;
//				} else {
//					return null;
//				}
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	/**
	 * rotate photo
	 *
	 * @return
	 */
//	public static String cameraPhotoRotate(String savePath, int screenWidth,
//			int screenHeight) {
//		Bitmap bm = null;
//		try {
//			int nRotate = ImageUtils.getCameraPhotoRotateValue(savePath);
//			bm = ImageUtils.loadBitmapInSampleSize(savePath, screenWidth,
//					screenHeight, nRotate);
//			ImageUtils.saveBitmapToFileJPEG(bm, savePath);
//		} catch (OutOfMemoryError e) {
//			e.printStackTrace();
//		} finally {
//			if (bm != null) {
//				bm.recycle();
//				bm = null;
//			}
//		}
//		return savePath;
//	}

	/**
	 *
	 * uri content://路径转成file://
	 *
	 * @author meizhen.xu
	 * @param uri
	 * @param context
	 * @return
	 */
	public static String getUriToPath(Uri uri, Context context) {
		String img_path = "";
		Cursor actualimagecursor = null;
		try {

			String[] proj = { Images.Media.DATA };

			actualimagecursor = context.getContentResolver().query(uri, proj,
					null, null, null);

			int actual_image_column_index = actualimagecursor
					.getColumnIndexOrThrow(Images.Media.DATA);

			actualimagecursor.moveToFirst();

			img_path = actualimagecursor.getString(actual_image_column_index);

		} catch (Exception e) {

		} finally {
			if (actualimagecursor != null) {
				actualimagecursor.close();
			}
		}
		return img_path;
	}

	public static void deleteFile(String strPath) {
		if (strPath == null) {
			return;
		}
		File f = new File(strPath);
		if (f != null && f.exists()) {
			f.delete();
		}
	}

//	public static synchronized void writeLog(String message) {
//		if (TextUtils.isEmpty(getPathLogFile())) {
//			return;
//		}
//
//		// upload log
//		try {
//			long size = getFileSize(getPathLogFile());
//			if (size >= 1024.0 * 1024.0) {// 0.5M
//				new File(getPathLogFile()).renameTo(new File(getPathLogRoot()
//						+ "/" + System.currentTimeMillis() + ".txt"));
//				return;
//			}
//
//			if (!new File(getPathLogFile()).exists()) {
//				try {
//					new File(getPathLogFile()).createNewFile();
//				} catch (Throwable e) {
//					return;
//				}
//			}
//
//			FileOutputStream fout = null;
//
//			fout = new FileOutputStream(getPathLogFile(), true);
//
//			byte[] bytes = message.getBytes();
//			fout.write("\n".getBytes());
//			fout.write(bytes);
//			fout.flush();
//			fout.close();
//
//		} catch (Throwable t) {
//		}
//	}
//
//	public static String getAvatarImagesCachePath() {
//		if (TextUtils.isEmpty(PATH_AVATAR_IMAGES_CACHE)
//				|| PATH_AVATAR_IMAGES_CACHE.contains("null")) {
//			PATH_AVATAR_IMAGES_CACHE = getPathRoot()
//					+ AppGlobal.SDCARD_AVATAR_IMAGE_CACHE_PATH;
//		}
//		return PATH_AVATAR_IMAGES_CACHE;
//	}

//	public static String getPathRoot() {
//		if (TextUtils.isEmpty(PATH_ROOT) || PATH_ROOT.contains("null")) {
//			PATH_ROOT = DeviceUtil.getSDCardPath() + AppGlobal.ROOT_SDCARD_PATH;
//		}
//		return PATH_ROOT;
//	}

//	public static String getPathLogRoot() {
//		if (TextUtils.isEmpty(PATH_LOG_ROOT) || PATH_LOG_ROOT.contains("null")) {
//			PATH_LOG_ROOT = getPathRoot() + "/UILog";
//		}
//		return PATH_LOG_ROOT;
//	}

//	public static String getPathLogFile() {
//		if (TextUtils.isEmpty(PATH_LOG_FILE) || PATH_LOG_FILE.contains("null")) {
//			PATH_LOG_FILE = getSDCardLogPath() + "/log.txt";
//		}
//		return PATH_LOG_FILE;
//	}

//	public static String getRegistUserIcon() {
//		if (TextUtils.isEmpty(PATH_REGIST_USER_ICON)
//				|| PATH_REGIST_USER_ICON.contains("null")) {
//			PATH_REGIST_USER_ICON = getSDCardTempPath() + "/"
//					+ AppGlobal.TEMP_IMG_FILE_NAME;
//		}
//
//		return PATH_REGIST_USER_ICON;
//	}

//	public static String getSDCardLogPath() {
//		// create dir
//		String logPath = getPathLogRoot();
//		createDirs(logPath);
//		return logPath;
//	}

	public static void createDirs(String path) {
		File f = new File(path);
		f.mkdirs();
	}

	/**
	 * 从assets目录中复制整个文件夹内容
	 *
	 * @param context
	 *            Context 使用CopyFiles类的Activity
	 * @param oldPath
	 *            String 原文件路径 如：/aa
	 * @param newPath
	 *            String 复制后路径 如：xx:/bb/cc
	 */
	public static void copyFilesFassets(Context context, String oldPath,
                                        String newPath) {
		try {
			String fileNames[] = context.getAssets().list(oldPath);// 获取assets目录下的所有文件及目录名
			if (fileNames.length > 0) {// 如果是目录
				File file = new File(newPath);
				file.mkdirs();// 如果文件夹不存在，则递归
				for (String fileName : fileNames) {
					copyFilesFassets(context, oldPath + "/" + fileName, newPath
							+ "/" + fileName);
				}
			} else {// 如果是文件
				InputStream is = context.getAssets().open(oldPath);
				FileOutputStream fos = new FileOutputStream(new File(newPath));
				byte[] buffer = new byte[1024];
				int byteCount = 0;
				while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
																// buffer字节
					fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
				}
				fos.flush();// 刷新缓冲区
				is.close();
				fos.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 如果捕捉到错误则通知UI线程
		}
	}

	public interface CacheCleanedCallback {
		public void onCacheCleaned();
	}

//	public static void deleteCacheFiles(final CacheCleanedCallback callback) {
//		CustomAsyncTask<Void, Void, Void> task = new CustomAsyncTask<Void, Void, Void>() {
//			@Override
//			protected Void doInBackground(Void... params) {
//				String rootPath = DeviceUtil.getSDCardPath();
//				if (!TextUtils.isEmpty(rootPath)) {
//					delete(getDefaultStoragePath(MyApplication.mContext));
//				}
//				return null;
//			}
//
//			@Override
//			protected void onPostExecute(Void result) {
//				if (callback != null) {
//					callback.onCacheCleaned();
//				}
//			}
//		};
//
//		if (DeviceUtil.hasHoneycomb()) {
//			task.executeOnExecutor(CustomAsyncTask.THREAD_POOL_EXECUTOR);
//		} else {
//			task.execute();
//		}
//
//	}

	/**
	 * 复制单个文件
	 *
	 * @param oldPathFile
	 *            准备复制的文件源
	 * @param newPathFile
	 *            拷贝到新绝对路径带文件名
	 * @return
	 */
	public static boolean copyFile(String oldPathFile, String newPathFile) {
		try {
			int byteread = 0;
			File oldfile = new File(oldPathFile);
			File newFile = new File(newPathFile);
			if (oldfile.exists()) { // 文件存在时
				if (!newFile.exists()) {
					FileUtil.createFile(newFile);
				}
				InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPathFile);
				byte[] buffer = new byte[1024];
				while ((byteread = inStream.read(buffer)) != -1) {
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.flush();
				fs.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			delele(newPathFile);
			return false;
		}
	}

	/**
	 * 创建文件
	 *
	 * @param file
	 *            路径名
	 * @return void
	 */
	public static void createFile(File file) {
		// File file = new File(pathName);
		if (!file.getParentFile().exists()) {
			if (file.getParentFile().mkdirs()) {
				try {
					if (file.createNewFile()) {
						return;
					} else {
						return;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				return;
			}
		} else {
			try {
				if (file.createNewFile()) {
					return;
				} else {
					return;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 删除文件或文件夹
	 *
	 * @param filePath
	 * @return
	 */
	public static boolean delele(String filePath) {
		if (TextUtils.isEmpty(filePath)) {
			return true;
		}

		File file = new File(filePath);
		if (!file.exists()) {
			return true;
		}
		if (file.isDirectory()) {
			return delFolder(filePath);
		} else {
			return delFile(filePath);
		}
	}

	/**
	 * 删除文件夹
	 *
	 * @param folderPath
	 *            文件夹完整绝对路径
	 * @return
	 */
	private static boolean delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			myFilePath.delete(); // 删除空文件夹
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 删除文件
	 *
	 * @param filePath
	 * @return
	 */
	private static boolean delFile(String filePath) {
		try {
			File myDelFile = new File(filePath);
			myDelFile.delete();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 删除指定文件夹下所有文件
	 *
	 * @param path
	 *            文件夹完整绝对路径
	 * @return
	 * @return
	 */
	private static boolean delAllFile(String path) {
		boolean bea = false;
		File file = new File(path);
		if (!file.exists()) {
			return bea;
		}
		if (!file.isDirectory()) {
			return bea;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				bea = true;
			}
		}
		return bea;
	}

	/**
	 * 扫描图片文件
	 *
	 * @param ctx
	 * @param imagePath
	 *            图片文件的路径
	 * @return Uri
	 *
	 */
	public static Uri scanneImage(Context ctx, String imagePath) {
		try {
			final ContentValues values = new ContentValues(2);
			values.put(MediaStore.Video.Media.MIME_TYPE, "image/jpeg");
			values.put(MediaStore.Video.Media.DATA, imagePath);
			final Uri uri = ctx.getContentResolver().insert(
					Images.Media.EXTERNAL_CONTENT_URI, values);
			ctx.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
					Uri.parse("file://" + imagePath)));
			return uri;
		} catch (Exception e) {
			return null;
		}
	}


	public static void delete(String filePath) {
		if (TextUtils.isEmpty(filePath)) {
			return;
		}
		delete(new File(filePath));
	}

	public static void delete(File file) {
		if (file.isFile()) {
			file.delete();
			return;
		}

		if (file.isDirectory()) {
			File[] childFiles = file.listFiles();
			if (childFiles == null || childFiles.length == 0) {
				file.delete();
				return;
			}

			for (int i = 0; i < childFiles.length; i++) {
				delete(childFiles[i]);
			}
			file.delete();
		}
	}

	public static boolean copy(String oldPath, String newPath) {
		File file = new File(oldPath);
		if (file.isDirectory()) {
			return copyFolder(oldPath, newPath);
		} else {
			return copyFile(oldPath, newPath);
		}
	}

	/**
	 * 复制整个文件夹的内容
	 * 
	 * @param oldPath
	 *            准备拷贝的目录
	 * @param newPath
	 *            指定绝对路径的新目录
	 * @return
	 */
	private static boolean copyFolder(String oldPath, String newPath) {
		try {
			new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}
				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(new File(
							newPath, temp.getName()));
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					input.close();
					output.flush();
					output.close();
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
				}
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
//
//	public static String getFeedbackZip() {
//		delete(getSDCardFeedbackTempPath());
//
//		// copy日志文件到临时目录
//		FileUtil.copy(FileUtil.getPathLogFile(),
//				FileUtil.getSDCardFeedbackTempPath() + File.separator
//						+ "log.txt");
//
//		return FileUtil.getSDCardFeedbackTempPath() + File.separator
//				+ "log.txt";
//		// writeDbInfoFile();
//		// copy sys db
//
//	}

	public static String getTmpDirSize(Context mContext) {
		return getFileOrFilesSize(getDefaultStoragePath(mContext)
				.getPath());
	}

	public static final int SIZETYPE_B = 1;// 获取文件大小单位为B的double值
	public static final int SIZETYPE_KB = 2;// 获取文件大小单位为KB的double值
	public static final int SIZETYPE_MB = 3;// 获取文件大小单位为MB的double值
	public static final int SIZETYPE_GB = 4;// 获取文件大小单位为GB的double值

	/**
	 * 获取文件指定文件的指定单位的大小
	 * 
	 * @param filePath
	 *            文件路径
	 *            获取大小的类型1为B、2为KB、3为MB、4为GB
	 * @return double值的大小
	 */
	public static String getFileOrFilesSize(String filePath) {
		File file = new File(filePath);
		long blockSize = 0;
		try {
			if (file.isDirectory()) {
				blockSize = getFileSizes(file);
			} else {
				blockSize = getFileSize(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("获取文件大小", "获取失败!");
		}
		return FormetFileSize(blockSize);
	}

	/**
	 * 获取指定文件大小
	 * 
	 * @return
	 * @throws Exception
	 */
	private static long getFileSize(File file) throws Exception {
		long size = 0;
		if (file.exists()) {
			FileInputStream fis = null;
			fis = new FileInputStream(file);
			size = fis.available();
		} else {
			file.createNewFile();
			Log.e("获取文件大小", "文件不存在!");
		}
		return size;
	}

	/**
	 * 获取指定文件夹
	 * 
	 * @param f
	 * @return
	 * @throws Exception
	 */
	private static long getFileSizes(File f) throws Exception {
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSizes(flist[i]);
			} else {
				size = size + getFileSize(flist[i]);
			}
		}
		return size;
	}

	/**
	 * 转换文件大小
	 * 
	 * @param fileS
	 * @return
	 */
	private static String FormetFileSize(long fileS) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		String wrongSize = "0B";
		if (fileS == 0) {
			return wrongSize;
		}
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "KB";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "MB";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "GB";
		}
		return fileSizeString;
	}

	/**
	 * 转换文件大小,指定转换的类型
	 * 
	 * @param fileS
	 * @param sizeType
	 * @return
	 */
	private static double FormetFileSize(long fileS, int sizeType) {
		DecimalFormat df = new DecimalFormat("#.00");
		double fileSizeLong = 0;
		switch (sizeType) {
		case SIZETYPE_B:
			fileSizeLong = Double.valueOf(df.format((double) fileS));
			break;
		case SIZETYPE_KB:
			fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
			break;
		case SIZETYPE_MB:
			fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
			break;
		case SIZETYPE_GB:
			fileSizeLong = Double.valueOf(df
					.format((double) fileS / 1073741824));
			break;
		default:
			break;
		}
		return fileSizeLong;
	}
}
